﻿'This program is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 3 of the License, or
'    (at your option) any later version.

'    This program is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with this program.  If not, see <http://www.gnu.org/licenses/>.


Public Class Update

    'Checking for internet connection
    Function CheckConnection()
        Dim isAvailable = My.Computer.Network.IsAvailable
        Return isAvailable
    End Function

    'Check if there is a newer version
    Function CheckIfNewVersion(ByVal NowVersion As String, ByVal VersionFileURL As Uri)
        Dim fileName As String
        fileName = My.Computer.FileSystem.GetTempFileName() & "version"
        My.Computer.Network.DownloadFile(VersionFileURL.ToString, fileName)
        Dim NewVerion As String
        NewVerion = My.Computer.FileSystem.ReadAllText(fileName)
        If NowVersion <> NewVerion Then
            Return True
        Else
            Return False
        End If
    End Function

    'Start the download of update file (SFX archieve)
    Function DownloadUpdatedFile(ByVal UpdatedFileURL As Uri)
        Dim fileName As String
        fileName = My.Computer.FileSystem.GetTempFileName() & "update"
        Try
            My.Computer.Network.DownloadFile(UpdatedFileURL.ToString, fileName & ".exe")
        Catch ex As Exception
            Return ex.ToString
        End Try
        Return fileName
    End Function

End Class
